package com.heima.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import com.hema.feign.apis.Wemedia.IWemediaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserRealnameServiceImpl implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public PageResponseResult list(AuthDto dto) {
        if (dto==null){
            return new PageResponseResult(0,0,0);
        }
        dto.checkParam();
        Integer dtoPage = dto.getPage();
        Integer dtoSize = dto.getSize();
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getStatus()!=null,ApUserRealname::getStatus,dto.getStatus());
        Page<ApUserRealname> page = new Page<>();
        Page<ApUserRealname> apUserRealnamePage = apUserRealnameMapper.selectPage(page, wrapper);
        Long total = apUserRealnamePage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage,dtoSize,total.intValue());
        pageResponseResult.setData(apUserRealnamePage.getRecords());
        return pageResponseResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult auth(AuthDto dto, Integer type) {
        // 1.入参检测
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());
        if (apUserRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        apUserRealname.setStatus((short)(type==1?9:2));
        apUserRealname.setId(dto.getId());
        apUserRealname.setReason(dto.getMsg());
        apUserRealnameMapper.updateById(apUserRealname);
        if (type==1){
            String name = apUserRealname.getName();
            Map<String, String> map = new HashMap<>();
            map.put("name",name);
            ResponseResult responseResult = iWemediaClient.add(map);
            if (responseResult==null||responseResult.getCode()!=200){
                throw new RuntimeException("自媒体用户创建失败");
            }
            ApUser apUser=new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            int updateResult = apUserMapper.updateById(apUser);
            if (updateResult<1){
                throw new RuntimeException("APP用户更新失败");
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
